package com.dly.hope.dao;

import com.dly.hope.modle.QAccountRole;
import com.dly.hope.modle.QRole;
import com.dly.hope.modle.Role;
import com.dly.hope.pageUtils.Page;
import com.dly.hope.pageUtils.PageRo;
import com.dly.hope.vo.RoleShow;
import com.querydsl.core.types.Projections;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author linyu.dai
 * @project myserve
 * @description
 * @creat 2022/11/1 10:55:20
 */
@Repository
public class RoleDao extends BaseDao{

    public QRole basePath(){
        return QRole.role;
    }


    public List<Role> getRole(String accountId){

        QAccountRole accountRole = QAccountRole.accountRole;

        return queryFactory.selectFrom(basePath()).
                leftJoin(accountRole).on(accountRole.roleId.eq(basePath().id).and(efficient(accountRole.dataStatus)))
                .where(efficient(basePath().dataStatus).and(accountRole.accountId.eq(accountId))).fetch();
    }

    public Page<RoleShow> getAllRole(PageRo pageRo){
        Page<RoleShow> target = new Page<>(pageRo);

        List<RoleShow> status = queryFactory.select(Projections.bean(
                        RoleShow.class,
                        basePath().id,
                        basePath().roleName,
                        basePath().dataStatus.as("status"),
                        basePath().createDate
                )).from(basePath())
                //几条
                .limit(pageRo.getPageSize())
                //偏移量
                .offset((long) pageRo.getPageSize() * (pageRo.getPageNumber() - 1))
                .orderBy(basePath().createDate.desc())
                .fetch();

        target.setContent(status);

        return target;
    }

}
